package webapi

import (
	"errors"
	SqliteDB "webSer/sqlitedb"
)

//用户管理
type User struct{}

type UserLoginArgs struct {
	User     string `json:"user"`
	PassWord string `json:"password"`
}
type UserLoginResult struct {
	Auth   string `json:"auth"`
	UserID string `json:"userid"`
}
type UserGetAtruct struct {
	Option string `json:"option"`
	Name   string `json:"name,omitempty"`
	Value  string `json:"value,omitempty"`
}
type UserGetResultAtruct struct {
	Data interface{} `json:"data"`
}

//用户登录
func (u *User) Login(args *UserLoginArgs, reply *UserLoginResult) error {
	Log.Debug("开始验证密码.")
	userData := SqliteDB.User{}

	errInt := userData.CheckPasswd(args.User, args.PassWord)
	if errInt != 0 {
		return errors.New("账号密码错误")
	}
	Log.Debug("开始设置Auth,默认七天")
	errInt, auth, UserID := userData.SetAuth(args.User, "7") //设置为七天
	if errInt != 0 {
		return errors.New("设置账户验证错误")
	}
	reply.Auth = auth
	reply.UserID = UserID
	return nil
}

//获取用户信息
func (u *User) Get(args *UserGetAtruct, reply *UserGetResultAtruct) error {
	switch args.Option {
	case "GetInfo":
		userData := SqliteDB.User{}
		reply.Data = userData.GetUserInfo(args.Name)
		break
	default:
		return errors.New("参数错误")
	}

	return nil
}

//设置单项用户信息

func (u *User) SetField(args *UserGetAtruct, reply *UserGetResultAtruct) error {
	if args.Name == "" || args.Option == "" {
		return errors.New("参数错误")
	}
	userData := SqliteDB.User{}
	switch args.Option {
	case "CUserName":
		err := userData.SetOneInfo(args.Name, "cusername", args.Value)
		reply.Data = "ok"
		return err
	case "PNumber":
		err := userData.SetOneInfo(args.Name, "phonenumber", args.Value)
		reply.Data = "ok"
		return err
	case "email":
		err := userData.SetOneInfo(args.Name, "email", args.Value)
		reply.Data = "ok"
		return err
	}
	reply.Data = "ok"
	return nil

}
